import { Component, OnInit, ViewChild } from '@angular/core';
import { Router } from '@angular/router';
import { STComponent, STColumn, STChange, STRequestOptions, STData } from '@delon/abc/st';
import { SFComponent, SFSchema, SFDateWidgetSchema } from '@delon/form';
import { SearchDrawerService } from '@shared';
import { NzModalService } from 'ng-zorro-antd/modal';
import { BasicTableComponent } from 'src/app/routes/commom';
import { FreightAccountService } from '../../services/freight-account.service';
import { CwcBankCardManagementBindComponent } from '../bank-card-management/bind/bind.component';
import { CwcAccountManagementWithdrawDepositComponent } from './withdraw-deposit/withdraw-deposit.component';

@Component({
  selector: 'app-platform-account',
  templateUrl: './platform-account.component.html',
  styleUrls: ['../../../commom/less/commom-table.less', '../../../commom/less/common-table-bar.less']
})
export class PlatformAccountComponent extends BasicTableComponent implements OnInit {
  schema: SFSchema = this.initSF();
  columns: STColumn[] = this.initST();

  info: any = {};

  static: any = {};
  constructor(
    public service: FreightAccountService,
    private router: Router,
    private nzModalService: NzModalService,
    public modal: NzModalService,
    public searchDrawerService: SearchDrawerService
  ) {
    super(searchDrawerService);
  }

  ngOnInit(): void {}

  search() {
    this.st?.load(1);
  }

  beforeReq = (requestOptions: STRequestOptions) => {
    if (this.sf) {
      Object.assign(requestOptions.body, { ...this.sf?.value });
    }
    this.loadStatistics(requestOptions.body);
    this.loadInfo();
    return requestOptions;
  };

  loadInfo() {
    const params = {};
    if (this.sf) {
      Object.assign(params, { ...this.sf?.value });
    }
    this.service.request(this.service.$api_get_platform_account_header, params).subscribe(res => {
      if (res) {
        this.info = res;
      }
    });
  }

  loadStatistics(params: any) {
    this.service.request(this.service.$api_get_platform_account_statistics, params).subscribe(res => {
      if (res) {
        this.static = res;
      }
    });
  }

  /**
   * 重置表单
   */
  resetSF() {
    this.sf.reset();
  }

  private initSF(): SFSchema {
    return {
      properties: {
        ltdId: {
          type: 'string',
          title: '网络货运人',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            allowClear: true,
            asyncData: () => this.service.getNetworkFreightForwarder()
          }
        },
        bankType: {
          type: 'string',
          title: '银行类型',
          enum: [
            { label: '全部', value: null },
            { label: '平安银行', value: '1' },
            { label: '浦发银行', value: '2' }
          ],
          ui: {
            widget: 'select',
            placeholder: '请选择'
          },
          default: null
        },
        virtualAccount: {
          type: 'string',
          title: '虚拟账户',
          ui: {
            placeholder: '请输入'
          }
        }
      }
    };
  }

  private initST(): STColumn[] {
    return [
      {
        title: '序号',
        className: 'text-center',
        width: '60px',
        format: (item: STData, col: STColumn, idx: number) => {
          return ((this.st.pi -1) * this.st.ps) + idx + 1 + ''
        } 
      },
      { title: '网络货运人', index: 'ltdName', width: 180 },
      { title: '银行类型', index: 'bankTypeLabel', width: 120 },
      { title: '虚拟账户', index: 'virtualAccount', width: 160 },
      {
        title: '平台账户可用余额',
        index: 'availableBalance',
        width: 180,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.availableBalance }) }
      },
      {
        title: '平台账户冻结余额',
        index: 'freezeBalance',
        width: 180,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.freezeBalance }) }
      },
      {
        title: '货主账户可用余额',
        index: 'shipperAvailableBalance',
        width: 180,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.shipperAvailableBalance }) }
      },
      {
        title: '货主账户冻结余额',
        index: 'shipperFreezeBalance',
        width: 180,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.shipperFreezeBalance }) }
      },
      {
        title: '司机账户可用余额',
        index: 'driverAvailableBalance',
        width: 180,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.driverAvailableBalance }) }
      },
      {
        title: '司机账户冻结余额',
        index: 'driverFreezeBalance',
        width: 180,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.driverFreezeBalance }) }
      },
      {
        title: '累计充值金额',
        index: 'rechargeBalance',
        width: 180,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.rechargeBalance }) }
      },
      {
        title: '货主累计提现金额',
        index: 'shipperWithdrawBalance',
        width: 180,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.shipperWithdrawBalance }) }
      },
      {
        title: '司机累计提现金额',
        index: 'driverWithdrawBalance',
        width: 180,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.driverWithdrawBalance }) }
      },
      {
        title: '操作',
        width: 120,
        className: 'text-center block-td',
        fixed: 'right',
        buttons: [
          {
            text: '查看明细 ',
            acl: { ability: ['FINANCIAL-PLATFORM-view'] },
            click: item =>
              this.router.navigate(['/financial-management/platform-account/detail/' + item.id], {
                queryParams: { ltdId: item.ltdId, bankType: item.bankType, ltdName: `${item.ltdName}(${item.bankTypeLabel})` }
              })
          },
          {
            text: '提现',
            click: item => this.withdraw(item),
            acl: { ability: ['FINANCIAL-PLATFORM-withdraw'] },
            iif: _record => _record.bankType !== '1'
          },
          // {
          //   text: '绑定银行卡',
          //   click: item => this.bindBankcard(item)
          // },
          {
            text: '查看银行卡',
            click: item => this.viewBankcard(item),
            acl: { ability: ['FINANCIAL-PLATFORM-viewBankcard'] },
            iif: _record => _record.bankType !== '1'
          }
        ]
      }
    ];
  }

  /**
   * 绑定银行卡
   */
  bindBankcard(item: any) {
    const modalRef = this.modal.create({
      nzTitle: '绑定银行卡',
      nzContent: CwcBankCardManagementBindComponent,
      nzWidth: '40%',
      nzFooter: null,
      nzComponentParams: {
        i: item
      }
    });
    modalRef.afterOpen.subscribe(() => {});
    modalRef.afterClose.subscribe(result => {
      if (result) this.st.reload();
    });
  }

  /**
   * 查看银行卡
   */
  viewBankcard(item: any) {
    this.router.navigate(['/financial-management/bank-card-management/index'], {
      queryParams: {
        ltdId: item?.ltdId,
        ltdName: item?.ltdName
      }
    });
  }

  exportList() {
    this.service.exportStart({ ...this.sf?.value, pageSize: -1 }, this.service.$api_get_exportPlatformAccountBalanceByOperator);
  }

  // 提现
  withdraw(record: any) {
    const modalRef = this.modal.create({
      nzTitle: '提现',
      nzWidth: '35%',
      nzContent: CwcAccountManagementWithdrawDepositComponent,
      nzMaskClosable: false,
      nzComponentParams: {
        record
      },
      nzFooter: null
    });
    modalRef.afterClose.subscribe(res => {
      if (res) {
        // this.getSummary();
        // this.withdrawTable.refresh();
      }
    });
  }
}
